package com.onlineMIS.ORM.DAO.chainS.finance;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;



import com.onlineMIS.ORM.DAO.chainS.user.ChainStoreDaoImpl;
import com.onlineMIS.ORM.DAO.chainS.user.ChainStoreService;
import com.onlineMIS.ORM.DAO.chainS.user.ChainUserInforService;
import com.onlineMIS.ORM.DAO.headQ.finance.FinanceBillImpl;
import com.onlineMIS.ORM.DAO.headQ.finance.FinanceCategoryImpl;
import com.onlineMIS.ORM.entity.chainS.user.ChainRoleType;
import com.onlineMIS.ORM.entity.chainS.user.ChainStore;
import com.onlineMIS.ORM.entity.chainS.user.ChainUserInfor;
import com.onlineMIS.ORM.entity.headQ.finance.FinanceBill;
import com.onlineMIS.ORM.entity.headQ.inventory.InventoryOrder;
import com.onlineMIS.action.headQ.finance.FinanceActionFormBean;
import com.onlineMIS.action.headQ.finance.FinanceActionUIBean;

@Service
public class FinanceChainService {
	@Autowired
	private FinanceBillImpl financeBillImpl;
	
	@Autowired
	private FinanceCategoryImpl financeCategoryImpl;
	
	@Autowired
	private ChainStoreService chainStoreService;
	
	@Autowired
	private ChainStoreDaoImpl chainStoreDaoImpl;
	
	/**
	 * to prepare the bean when create the finance bill
	 * @param formBean
	 * @param uiBean
	 */
	public void prepareSearchFinanceBill(FinanceActionFormBean formBean, ChainUserInfor userInfor){
		if (!ChainUserInforService.isMgmtFromHQ(userInfor)){
			int chainId = userInfor.getMyChainStore().getChain_id();
			ChainStore chainStore = chainStoreService.getChainStoreByID(chainId);
			formBean.setChainStore(chainStore);
		} else {
			ChainStore allChainStore = ChainStoreDaoImpl.getAllChainStoreObject();
			formBean.setChainStore(allChainStore);
		}
	}

	@Transactional
	public FinanceBill getFinanceHQBillById(int id, ChainUserInfor userInfor) {
		FinanceBill financeBill = financeBillImpl.get(id, true);
		if (validateFinance(financeBill, userInfor)){
		    financeBillImpl.initialize(financeBill);
		
		    financeBill.putSetToList();
		} else 
			financeBill = null;
		
		return financeBill;
	}

	/**
	 * 验证是否有权限查询这个单据
	 * 	 * 验证当前用户如果不是总部管理人员，就只能看当前连锁店和子连锁店单据
	 * @param financeBill
	 * @param userInfor
	 * @return
	 */
	private boolean validateFinance(FinanceBill financeBill, ChainUserInfor userInfor) {
		if (financeBill.getStatus() != financeBill.STATUS_COMPLETE){
			return false;
		} else {
			if (ChainUserInforService.isMgmtFromHQ(userInfor))
				return true;
			else {
				Set<Integer> clientIds = chainStoreDaoImpl.getStoreAndChildrenClientIds(userInfor.getMyChainStore().getChain_id());
				if (!clientIds.contains(financeBill.getCust().getId()))
					return false;
				else {
					if (userInfor.getRoleType().getChainRoleTypeId() != ChainRoleType.CHAIN_OWNER) {
						
						financeBillImpl.evict(financeBill);
						financeBill.setPreAcctAmt(0);
						financeBill.setPostAcctAmt(0);
					}
					return true;
				}
					
			}
		}
	}

	/**
	 * 准备帐户流搜索的界面
	 * @param uiBean
	 * @param userInfor
	 */
	public void prepareSearchAcctFlowUI(FinanceActionUIBean uiBean, FinanceActionFormBean formBean,
			ChainUserInfor userInfor) {
		//1. 准备页面
		if (!ChainUserInforService.isMgmtFromHQ(userInfor)){
			int chainId = userInfor.getMyChainStore().getChain_id();
			ChainStore chainStore = chainStoreService.getChainStoreByID(chainId);
			formBean.setChainStore(chainStore);
		} 
		
	}
}
